import React, {useState, useContext} from 'react';
import {useMount, useSafeState} from 'ahooks';
import dayjs from 'dayjs';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
  NavigationContainer,
  useNavigationContainerRef,
} from '@react-navigation/native';
import {useSelector, useDispatch} from 'react-redux';
import {setRouteDate} from '@/store';
import GlobalContext from '@/GlobalContext';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
const NativeStack = createNativeStackNavigator();

import Icon from '@/Icon';
import Sys from './Sys';
import ERM from './ERM';
import Normal from './Normal';
import WxBus from './WxBus';
import DingDing from './DingDing';
import Alipay from './Alipay';
import Taobao from './Taobao';
import Jingdong from './Jingdong';
import OPlayer from './OPlayer';

export default () => {
  const {eventEmitterGlobal, setGlobalContext} = useContext(GlobalContext);

  const dispatch = useDispatch();
  const routeData = useSelector(state => state.routeData.value);
  const accountInfo = useSelector(state => state.accountInfo.value);
  const isSignedIn = !!accountInfo?.account_id;

  const navigationRef = useNavigationContainerRef();
  const onRouteChange = () => {
    const route = navigationRef.getCurrentRoute();
    // const state = navigationRef.getState();
    dispatch(setRouteDate(route));
    try {
      const v = route.name;
      AsyncStorage.setItem('pageRouteName', v);
    } catch (e) {
      console.log('保存页面路径异常', e.message);
    }
  };

  const [defaultRouteName, setDefaultRouteName] = useSafeState();

  // useMount(async () => {
  //   const v = await AsyncStorage.getItem('pageRouteName');
  //   setDefaultRouteName(v || '');
  //   if (!v) {
  //     return;
  //   }
  //   setTimeout(() => {
  //     if (navigationRef.isReady()) {
  //       console.log('跳转上一次路由', v);
  //       setGlobalContext({navigationRef: navigationRef});

  //       try {
  //         // navigationRef?.navigate('Normal');
  //         // navigationRef?.navigate('Normal', {screen: 'Normal/DeviceInfo/Screen'});
  //         navigationRef?.navigate('Normal', {screen: 'Normal/Powers/Basic'});

  //         // navigationRef?.navigate('WxBus', {screen: 'WxBus/Email'});
  //       } catch (e) {
  //         // navigationRef?.navigate('Home');
  //       }
  //     }
  //   });
  // });
  useMount(async () => {
    const v = await AsyncStorage.getItem('pageRouteName');
    setDefaultRouteName(v || '');
  });
  if (defaultRouteName === undefined) {
    return <></>;
  }

  return (
    <NavigationContainer
      ref={navigationRef}
      onReady={() => {
        // onRouteChange();
        setGlobalContext({navigationRef: navigationRef});
        // navigationRef?.navigate('Normal');
        // navigationRef?.navigate('Normal', {screen: 'Normal/Powers/Basic'});
        // navigationRef?.navigate('Normal', {screen: 'Normal/Powers/Camera'});
        // navigationRef?.navigate('Normal', {screen: 'Normal/Powers/CameraClick'});
        // navigationRef?.navigate('Normal', {screen: 'Normal/Powers/SDCard'});
        // navigationRef?.navigate('Normal', {screen: 'Normal/Powers/ReadBinFile'});
      }}
      onStateChange={state => {
        onRouteChange();
      }}>
      <NativeStack.Navigator
        screenOptions={{headerShown: false, animation: 'slide_from_right'}}>
        <NativeStack.Screen name="Home" component={Sys} />
        <NativeStack.Screen
          name="ERM"
          component={ERM}
          options={{headerShown: false, title: '企业资源管理'}}
        />
        <NativeStack.Screen
          name="Normal"
          component={Normal}
          options={{headerShown: false, title: '常用功能'}}
        />
        <NativeStack.Screen name="WxBus" component={WxBus} />
        <NativeStack.Screen name="DingDing" component={DingDing} />
        <NativeStack.Screen name="Alipay" component={Alipay} />
        <NativeStack.Screen name="Taobao" component={Taobao} />
        <NativeStack.Screen name="Jingdong" component={Jingdong} />
        <NativeStack.Screen name="OPlayer" component={OPlayer} />
      </NativeStack.Navigator>
    </NavigationContainer>
  );
};
